package cn.outsourcing.supermarket.service.impl;

import cn.outsourcing.supermarket.common.constants.CommonConstants;
import cn.outsourcing.supermarket.common.exception.SupermarketException;
import cn.outsourcing.supermarket.common.utils.DataUtils;
import cn.outsourcing.supermarket.common.utils.JwtUtils;
import cn.outsourcing.supermarket.converter.HiolabsAdminConverter;
import cn.outsourcing.supermarket.dto.AdminMemoDTO;
import cn.outsourcing.supermarket.dto.OnlyIdPayloadDTO;
import cn.outsourcing.supermarket.dto.backstage.BackstageAdminInfoDTO;
import cn.outsourcing.supermarket.dto.backstage.BackstageAdminUserInfoDTO;
import cn.outsourcing.supermarket.dto.backstage.BackstageUserInfoDTO;
import cn.outsourcing.supermarket.mapper.HiolabsAdminMapper;
import cn.outsourcing.supermarket.mapper.HiolabsOrderMapper;
import cn.outsourcing.supermarket.po.HiolabsAdmin;
import cn.outsourcing.supermarket.po.HiolabsOrder;
import cn.outsourcing.supermarket.service.HiolabsAdminService;
import cn.outsourcing.supermarket.vo.backstage.LoginResultVO;
import cn.outsourcing.supermarket.vo.backstage.UserInfoVO;
import cn.outsourcing.supermarket.vo.hiolabs.HiolabsAdminVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务实现类
 *
 * @author gzkemays
 * @since 2021-10-25
 */
@Service
public class HiolabsAdminServiceImpl extends ServiceImpl<HiolabsAdminMapper, HiolabsAdmin>
    implements HiolabsAdminService {
  @Resource HiolabsAdminMapper adminMapper;
  @Resource HiolabsOrderMapper orderMapper;
  @Resource HttpServletRequest request;

  @Override
  public LoginResultVO login(BackstageUserInfoDTO userInfoDTO) {
    HiolabsAdmin admin =
        adminMapper.selectOne(
            Wrappers.<HiolabsAdmin>lambdaQuery()
                .eq(HiolabsAdmin::getUsername, userInfoDTO.getUsername()));
    if (admin == null) {
      throw new SupermarketException(CommonConstants.REQUEST_FAIL_CODE, "无该账号");
    }
    if (admin
        .getPassword()
        .equals(DataUtils.saltMd5Str(userInfoDTO.getPassword(), admin.getPasswordSalt()))) {
      Map<String, Object> map = new HashMap<>(1);
      map.put("id", admin.getId());
      return LoginResultVO.builder()
          .token(JwtUtils.settings().build().createTokenByMap(map))
          .userInfo(UserInfoVO.builder().id(admin.getId()).username(admin.getUsername()).build())
          .build();
    }
    throw new SupermarketException(CommonConstants.REQUEST_FAIL_CODE, "密码错误");
  }

  @Override
  public String checkLogin() {
    String token = JwtUtils.getTokenByRequest(request);
    return JwtUtils.settings().build().isExpiration(token) ? "false" : "OK";
  }

  @Override
  public Integer orderMemo(AdminMemoDTO dto) {
    HiolabsOrder hiolabsOrder = orderMapper.selectById(dto.getId());
    hiolabsOrder.setAdminMemo(dto.getText());
    return orderMapper.updateById(hiolabsOrder);
  }

  @Override
  public List<HiolabsAdminVO> getAdminList() {
    return HiolabsAdminConverter.INSTANCE.convertToVoList(
        adminMapper.selectList(Wrappers.emptyWrapper()));
  }

  @Override
  public HiolabsAdminVO getAdminInfo(OnlyIdPayloadDTO dto) {
    return HiolabsAdminConverter.INSTANCE.convertToVo(adminMapper.selectById(dto.getId()));
  }

  @Override
  public Integer adminSave(BackstageAdminInfoDTO dto) {
    BackstageAdminUserInfoDTO user = dto.getUser();
    String newPwd = DataUtils.saltMd5Str(user.getNewpassword(), user.getPassword_salt());
    HiolabsAdmin admin = new HiolabsAdmin();
    admin.setId(user.getId());
    admin.setPassword(newPwd);
    admin.setUsername(user.getUsername());
    admin.setLastLoginTime(user.getLast_login_time());
    return adminMapper.updateById(admin);
  }
}
